package com.animal.service.impl;

import com.animal.entity.AnCollect;
import com.animal.entity.AnComment;
import com.animal.entity.AnInvitation;
import com.animal.mapper.AnCommentMapper;
import com.animal.mapper.AnInvitationMapper;
import com.animal.mapper.AnProductMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.service.AnCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author animal
 * @since 2022-01-29
 */
@Service
public class AnCommentServiceImpl extends ServiceImpl<AnCommentMapper, AnComment> implements AnCommentService {
@Autowired
AnCommentMapper anCommentMapper;
@Autowired
AnInvitationMapper anInvitationMapper;
@Autowired
    AnProductMapper anProductMapper;
@Autowired
    AnUserMapper anUserMapper;
    @Override
    public List<AnComment> findAll(int size,int current) {
        Page<AnComment> userPages = new Page<>(size,current);
        Page<AnComment> userPagess=  anCommentMapper.selectPage(userPages,null);
        List<AnComment> list=userPagess.getRecords();
        for(AnComment a:list){
            if(a.getInId()!=null){
                a.setAnInvitation(anInvitationMapper.selectById(a.getInId()));
            }
            if(a.getPrId()!=null){
                a.setAnProduct(anProductMapper.selectById(a.getPrId()));
            }
            if(a.getUserId()!=null){
                a.setAnUser(anUserMapper.selectById(a.getUserId()));
            }
        }
        return list;
    }

    @Override
    public List<AnComment> findByUserId(int size, int current, int userId) {
        Page<AnComment> userPages = new Page<>(size,current);
        QueryWrapper<AnComment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Page<AnComment> userPagess=  anCommentMapper.selectPage(userPages,wrapper);
        List<AnComment> list=userPagess.getRecords();
        for(AnComment a:list){
            if(a.getInId()!=null){
                a.setAnInvitation(anInvitationMapper.selectById(a.getInId()));
            }
            if(a.getPrId()!=null){
                a.setAnProduct(anProductMapper.selectById(a.getPrId()));
            }
            if(a.getUserId()!=null){
                a.setAnUser(anUserMapper.selectById(a.getUserId()));
            }
        }
        return list;
    }

    public List<AnComment> findByPrId(int size, int current, int userId) {
        Page<AnComment> userPages = new Page<>(size,current);
        QueryWrapper<AnComment> wrapper = new QueryWrapper<>();
        wrapper.eq("pr_id",userId);
        Page<AnComment> userPagess=  anCommentMapper.selectPage(userPages,wrapper);
        List<AnComment> list=userPagess.getRecords();
        for(AnComment a:list){
            if(a.getInId()!=null){
                a.setAnInvitation(anInvitationMapper.selectById(a.getInId()));
            }
            if(a.getPrId()!=null){
                a.setAnProduct(anProductMapper.selectById(a.getPrId()));
            }
            if(a.getUserId()!=null){
                a.setAnUser(anUserMapper.selectById(a.getUserId()));
            }
        }
        return list;
    }
    public List<AnComment> findByAnId(int size, int current, int userId) {
        Page<AnComment> userPages = new Page<>(size,current);
        QueryWrapper<AnComment> wrapper = new QueryWrapper<>();
        wrapper.eq("an_id",userId);
        Page<AnComment> userPagess=  anCommentMapper.selectPage(userPages,wrapper);
        List<AnComment> list=userPagess.getRecords();
        for(AnComment a:list){
            if(a.getInId()!=null){
                a.setAnInvitation(anInvitationMapper.selectById(a.getInId()));
            }
            if(a.getPrId()!=null){
                a.setAnProduct(anProductMapper.selectById(a.getPrId()));
            }
            if(a.getUserId()!=null){
                a.setAnUser(anUserMapper.selectById(a.getUserId()));
            }
        }
        return list;
    }
    public List<AnComment> findByInId(int size, int current, int userId) {
        Page<AnComment> userPages = new Page<>(size,current);
        QueryWrapper<AnComment> wrapper = new QueryWrapper<>();
        wrapper.eq("in_id",userId);
        Page<AnComment> userPagess=  anCommentMapper.selectPage(userPages,wrapper);
        List<AnComment> list=userPagess.getRecords();
        for(AnComment a:list){
            if(a.getInId()!=null){
                a.setAnInvitation(anInvitationMapper.selectById(a.getInId()));
            }
            if(a.getPrId()!=null){
                a.setAnProduct(anProductMapper.selectById(a.getPrId()));
            }
            if(a.getUserId()!=null){
                a.setAnUser(anUserMapper.selectById(a.getUserId()));
            }
        }
        return list;
    }
}
